--[[

	GModRP - Real Garry's Mod Roleplay.
			By Slob187.
		

]]

local mingeprops = { "crane_frame", "huge", "bridge", "building", "chair", "explosive" }
local mingetools = { "paint", "trails", "dynamite", "turret", "duplicator", "ignite", "nocollide", "remover" }

if file.Exists( "GModRP/settings/blockedprops"..GMRP.version..".txt" ) then

	mingeprops = util.KeyValuesToTable( file.Read( "GModRP/settings/blockedprops"..GMRP.version..".txt" ) )
	
else
	
	file.Write( "GModRP/settings/blockedprops"..GMRP.version..".txt", util.TableToKeyValues( mingeprops ) )
	
end


if file.Exists( "GModRP/settings/blockedtools"..GMRP.version..".txt" ) then

	mingetools = util.KeyValuesToTable( file.Read( "GModRP/settings/blockedtools"..GMRP.version..".txt" ) )
	
else

	file.Write( "GModRP/settings/blockedtools"..GMRP.version..".txt", util.TableToKeyValues( mingetools ) )
	
end


local emeta = FindMetaTable( "Entity" )

function emeta:ghostOn()

	local er, eg, eb = self:GetColor()
	self:SetColor( er, eg, eb, 100 )
	self:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
	
end


function emeta:ghostOff()

	local er, eg, eb = self:GetColor()
	self:SetColor( er, eg, eb, 255 )
	self:SetCollisionGroup( COLLISION_GROUP_NONE )
		
end


function GMRP.GetEntMass( ent )

	local constr = constraint.GetAllConstrainedEntities( ent )	
	
	for k, v in pairs( constr ) do
	
		local phobj = v:GetPhysicsObject()
	
		if !phobj:IsValid() then return 0 end
		
		return phobj:GetMass()
		
	end	
	
end	


function GM:PhysgunPickup( ply, ent )

	if !self.BaseClass:PhysgunPickup( ply, ent ) then
	
		return false
		
	elseif ent:isOwnable() then
		
		return false
		
	end 	
	
	if Configuration["limitedphysgun"] == 1 then
	
		if GMRP.GetEntMass( ent ) > 300 then
		
			ply:RedNotify( "You can not lift this object because it weighs too much." )
			return false
			
		elseif ply:GetPos():Distance( ent:GetPos() ) > 100 then
		
			ply:RedNotify( "This object is too far away to pick up." )
			return false
			
		end
		
	end	
	
	ent:ghostOn()
	
	return true

end


function GM:PhysgunDrop( p, ent )

	ent:ghostOff()
	
end	


function GM:CanTool( p, tr, toolmode )
	
	local class = tr.Entity:GetClass()
	
	if !self.BaseClass:CanTool( p, tr, toolmode ) || tr.Entity:IsPlayer() then
	
		return false
		
	end
	
	if ( Configuration["toolrange"] == 1 ) then
	
		if ( p:GetPos():Distance( tr.HitPos ) > 100 ) then
		
			p:RedNotify( "This surface is too far away to use tools on." )
			return false
			
		end
		
	end
		
	if string.find( class, "door" ) then
	
		return false
		
	elseif table.HasValue( mingetools, toolmode ) then
	
		p:RedNotify( toolmode.." tool is disabled" )
		return false		
		
	elseif tr.Entity.IsItem || string.find( class, "vehicle" ) then
	
		if toolmode == "remover" then
	
			return false
			
		end

	elseif ( !p:isJob( "builder" ) ) && ( Configuration["alltool"] == 0 ) then

		if toolmode != "rope" then
		
			p:RedNotify( "Must be a builder to use tools other than rope." )
			
			return false
			
		end

	end	
	
	return true

end


function GM:PlayerSpawnVehicle( p )

	if Configuration["vehiclespawning"] == 0 then

		p:RedNotify( "Vehicle spawning is disabled." )
	
		return false
		
	end	

	return true
	
end	


function GM:PlayerSpawnRagdoll( p, model )

	if Configuration["ragdollspawning"] == 0 then

		p:RedNotify( "Ragdoll spawning is disabled." )
	
		return false
		
	end	

	return true
	
end	


function GM:PlayerSpawnEffect( p, model )

	if Configuration["effectspawning"] == 0 then

		p:RedNotify( "Effect spawning is disabled." )
	
		return false
		
	end	

	return true
	
end	


function GM:PlayerSpawnSWEP( p, classname )

	if Configuration["swepspawning"] == 0 then

		p:RedNotify( "SWEP spawning is disabled." )
	
		return false
		
	end	

	return true
	
end


concommand.Add( "gm_giveswep", function( p )

	if Configuration["swepspawning"] == 0 then

		p:RedNotify( "SWEP spawning is disabled." )
		
	end
	
end )
	

function GM:PlayerSpawnSENT( p, sent_type ) 

	if Configuration["sentspawning"] == 0 then

		p:RedNotify( "SENT spawning is disabled." )
	
		return false
		
	end	

	return true
	
end	


function GM:PlayerSpawnProp( ply, mdl )

	if !self.BaseClass:PlayerSpawnProp( ply, mdl ) then
	
		return false
		
	end	
		
	for k, v in pairs( mingeprops ) do
	
		if string.find( string.lower( mdl ), v ) then
		
			ply:RedNotify( "This prop is blocked." )
			
			return false
			
		end
		
	end				
		
	if Configuration["allbuild"] == 1 then
	
		return true
		
	elseif Configuration["allbuildandpay"] == 1 then

		if !ply:HasMoney( Configuration["propcost"] ) then
			
			return false
			
		end
	
		ply:TakeMoney( Configuration["propcost"] )
		ply:RedNotify( "Paid "..CUR..Configuration["propcost"].." to spawn a prop." )	
		
		return true
		
	elseif ( !ply:isJob( "builder" ) ) then
	
		ply:RedNotify( "Must be a builder to spawn props." )
		
		return false
		
	end
	
	if !ply:HasMoney( Configuration["propcost"] ) then
	
		return false
		
	end
	
	ply:TakeMoney( Configuration["propcost"] )
	ply:RedNotify( "Paid "..CUR..Configuration["propcost"].." to spawn a prop." )
	
	return true

end